<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>AJAX获取响应状态码与数据处理</h1>
    <input type="button" value="获取数据" onclick="get_data()">
    <script>
        function get_data(){
            let xhr = new XMLHttpRequest()

            xhr.open('get','http://httpbin.org/get')

            xhr.send()

            xhr.onreadystatechange = () =>{
                //判断ajax状态码是否为4
                if(xhr.readyState === 4){
                    if(xhr.status === 200){
                        // console.log(xhr.responseText)
                        //将数据转换成json类型
                        let data = JSON.parse(xhr.responseText)
                        console.log(data)
                    }else{
                        console.log(xhr.status)
                    }
                }else{
                    console.log(xhr.readyState)
                }
            }
        }

    </script>
</body>
</html>